package com.bskj.kuaifu.controller.web.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.WxTemplate;
import com.bskj.common.utils.HttpRequestUtil;
import com.bskj.common.utils.LogUtils;
import com.bskj.common.web.BaseController;
import com.bskj.kuaifu.service.IWxTemplateService;
import com.bskj.kuaifu.service.template.TemplateMessageUtil;

/**
 * Created by mocaijin on 21/12/2017.
 */
@Controller
@RequestMapping("api")
public class WxServiceApi extends BaseController {

    @Autowired
    private IWxTemplateService wxTemplateService;

    /**
     * 绑定手机,车牌
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("notifyTempMsg")
    public String notifyTempMsg(final Integer userId, final String templateKey) {
        LogUtils.debug(JSONObject.toJSONString(getRequest().getParameterMap()));
        final String appId = getRequest().getParameter("appId");
        final String openId = getRequest().getParameter("openId");

        new Thread(new Runnable() {
            @Override
            public void run() {
                WxTemplate wxTemplate = new WxTemplate();
                wxTemplate.setAppId(appId);
                wxTemplate.setTemplateKey(templateKey);
                try {
                    wxTemplate = wxTemplateService.get(wxTemplate);
                    Map<String,Object> paramMap = new HashMap<String,Object>();
                    paramMap.put("userId", userId);
                    TemplateMessageUtil.sendTemplateMessage(appId, openId, paramMap, wxTemplate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


        return "ok";

    }
    
    @ResponseBody
	@RequestMapping("noSensePayNotify")
	public String noSensePayNotify(HttpServletRequest request) throws Exception {
    	String str = HttpRequestUtil.getXmlStrFromRequest(request);
    	LogUtils.debug(str);
		return "ok";
	}

}
